//
//  DSUnitExerciseResultController.m
//  DSJY
//
//  Created by 冯华强 on 16/9/25.
//  Copyright © 2016年 dsjy. All rights reserved.
//  单元练习的结果提示

#import "DSUnitExerciseResultController.h"
#import "DSUnitExerciseResultViewModel.h"
#import "DSMyLearnInfo.h"
#import "DSUnitExercise.h"
#import "UIColor+Extension.h"
#import "UIImage+Extension.h"

@interface DSUnitExerciseResultController ()
#pragma mark - 拖线属性
//@property (weak, nonatomic) IBOutlet UILabel *nextLabel;
//标题
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
//赞的图标
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
//对的题
@property (weak, nonatomic) IBOutlet UIButton *correctBtn;
//错的题
@property (weak, nonatomic) IBOutlet UIButton *wrongBtn;
//继续
@property (weak, nonatomic) IBOutlet UIButton *nextButton;
//重来
@property (weak, nonatomic) IBOutlet UIButton *againButton;
//返回
@property (weak, nonatomic) IBOutlet UIButton *backButton;
//继续按钮的宽度
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *nextButtonConstW;
//对错按钮View的高度
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightViewConstH;
@end

@implementation DSUnitExerciseResultController

#pragma mark - 系统回调
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //设置UI
    [self setupUI];
}

#pragma mark - 设置UI
- (void)setupUI
{
    DSMyLearnInfo *myLearnInfo = self.exerciseResultVM.myLearnInfo;
    DSUnitExercise *unitExercise = self.exerciseResultVM.unitExercise;
    
    BOOL isStory = unitExercise.exerciseType == DSUnitExerciseTypeStory;
    if (isStory) { //故事会
        self.rightViewConstH.constant = 0;
        self.correctBtn.hidden = isStory;
        self.wrongBtn.hidden = isStory;
        
        NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%ld分", myLearnInfo.score]];
        [attrText addAttribute:NSFontAttributeName value:setFont14 range:NSMakeRange(attrText.length - 1, 1)];
        self.titleLabel.attributedText = attrText;
        
        if (!myLearnInfo.iflastunit) { //90分以上并且是最后一单元
            //显示继续按钮
            self.nextButtonConstW.constant = 80;
        }
        return;
    }
    
    //对错
    [self.correctBtn setTitle:[NSString stringWithFormat:@"%ld", self.exerciseResultVM.rightCount] forState:UIControlStateNormal];
    [self.wrongBtn setTitle:[NSString stringWithFormat:@"%ld", self.exerciseResultVM.wrongCount] forState:UIControlStateNormal];
    
    //提示语
    if (myLearnInfo.score >= 90) {
        self.imageView.hidden = NO;
        self.imageView.image = [UIImage imageWithGifName:@"good.gif"];
        self.titleLabel.textColor = [UIColor redColor];
        self.titleLabel.text = [NSString stringWithFormat:@"通关成功，成绩为%ld分 你真棒", myLearnInfo.score];
        if (!myLearnInfo.iflastunit) { //90分以上并且是最后一单元
            //显示继续按钮 
            self.nextButtonConstW.constant = 80;
        }
    }else{
        NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"本次成绩：%ld分", myLearnInfo.score]];
        [attrText addAttribute:NSFontAttributeName value:setFont14 range:NSMakeRange(attrText.length - 1, 1)];
        self.titleLabel.attributedText = attrText;
    }
}

#pragma mark - 设置事件
//重来
- (IBAction)resetBtnClick:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
    if ([self.delegate respondsToSelector:@selector(unitExerciseResultControllerDidResetBtnClick:)]) {
        [self.delegate unitExerciseResultControllerDidResetBtnClick:self];
    }
}

//返回
- (IBAction)backBtnClick:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
    if ([self.delegate respondsToSelector:@selector(unitExerciseResultControllerDidBackBtnClick:)]) {
        [self.delegate unitExerciseResultControllerDidBackBtnClick:self];
    }
}

//继续
- (IBAction)nextButtonClick:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
    if ([self.delegate respondsToSelector:@selector(unitExerciseResultControllerDidNextButtonClick:)]) {
        [self.delegate unitExerciseResultControllerDidNextButtonClick:self];
    }
}

@end
